class Http {
  constructor() {
    this.baseUrl = process.env.NODE_ENV == 'development' ? "/api" : "http://116.204.91.36:3000"
  }

  _request(config) {
    return new Promise(resolve => {
      uni.request({
        url: this.baseUrl + config.url,
        method: config.method,
        data: config.data,
        header: {
          'token': '121123',
          ...config.header
        },
        success: (res) => {
          resolve(res)
        },
        error:(err)=>{
          resolve(err)
        }
      });
    })
  }

  get({url, data}) {
    return this._request({method: 'get', url, data})
  }

  postParams({url, data}) {
    return this._request({
      method: 'post', url, data,
      header: {'content-type': 'application/x-www-form-urlencoded'}
    })
  }

  postBody({url, data}) {
    return this._request({
      method: 'post', url, data,
      header: {'content-type': 'application/json;charset=utf-8'}
    })
  }
}

export const http = new Http()
